<template>
  <div class="container" v-if="brandArea">
    <scroll-view class="scroll-top" scroll-x>
      <block v-for="(item, index) in brandArea.subs" :index="index" :key="index">
        <view class="box" :class="{active: item.display_name === currentBrandName}" @click="brandInfo(item.display_name)">
          <img :src="item.image + '?imageView2/1/w/120/h/120/q/75'"/>
        </view>
      </block>
    </scroll-view>
    <div class="current-head">
      当前品牌：{{currentBrandName}}
    </div>
    <van-row gutter="10">
      <div class="pro-list">
        <van-col span="12" v-for="(item, index) in productsList" :key="index" :index="index">
        <div class="pro-item" @click="goToInfo(item.id)"> 
        <img  v-if="item.images" class="pro-pic" mode="aspectFill" :src="item.images[0] + '?imageView2/1/w/320/h/320/q/75'"/>
        <div class="info">
          <div class="desc">{{item.title}}</div>
          <p class="price">￥{{item.price}}</p>
        </div>
      </div>
      </van-col>
      </div>
    </van-row>
  </div>
</template>
<script>
import * as CategoryAction from '@/api/category'
import * as ProductAction from '@/api/product'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      currentBrandName: '',
      name: '',
      page: 1,
      brandArea: null,
      productsList: []
    }
  },
  computed: {
    ...mapState(['me'])
  },
  onShareAppMessage (res) {
    return {
      title: this.shareTitle(),
      path: this.sharePath()
    }
  },
  onReachBottom () {
    this.page += 1
    wx.showNavigationBarLoading()
    this.fetchList()
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.fetchBrands()
    this.fetchList()
    wx.stopPullDownRefresh()
  },
  onLoad (query) {
    this.page = 1
    this.productsList = []
    wx.showNavigationBarLoading()
    wx.showShareMenu({ withShareTicket: true })
    this.title = query.title
    this.currentBrandName = query.name
    if (query.code) wx.setStorageSync('__inviteCode', query.code)
    this.fetchBrands()
    this.fetchList()
  },
  methods: {
    brandInfo (name) {
      wx.showNavigationBarLoading()
      this.page = 1
      this.currentBrandName = name
      this.productsList = []
      this.fetchList()
    },
    goToInfo (id) {
      wx.navigateTo({ url: '../product/detail?id=' + id })
    },
    shareTitle () {
      if (!this.brandArea) return
      if (!this.me) return `美控小管家向您推荐了${this.currentBrandName}品牌`
      if (this.me) return `${this.me.nickname}向您推荐了${this.currentBrandName}品牌`
    },
    sharePath () {
      if (!this.brandArea) return
      let url = '/pages/brand/area?name=' + this.currentBrandName + '&title=' + this.brandArea.name
      if (this.me && this.me.invite_code) {
        return url + '&code=' + this.me.invite_code
      } else {
        return url
      }
    },
    fetchList () {
      let tags = [this.title, this.currentBrandName]
      let params = {
        tags: tags,
        page: this.page
      }
      ProductAction.list(params).then(data => {
        if (data.data.length > 0) {
          this.productsList = this.productsList.concat(data.data)
        }
        wx.hideNavigationBarLoading()
      }).catch(err => console.log(err))
    },
    fetchBrands () {
      CategoryAction.all({r: Math.random()}).then(data => {
        for (let item of data) {
          if (item.name === this.title) {
            this.brandArea = item
            wx.setNavigationBarTitle({title: item.display_name})
          }
        }
      }).catch(err => console.log(err))
    }
  }
}
</script>

<style lang="less" scoped>
@theme-red: #E6648C;
.container {
  background: #F8F8F8;
  min-height: 1448rpx;
  padding-bottom:50rpx;
  .scroll-top {
    width: 100%;
    padding: 20rpx 0 0;
    white-space: nowrap;
    border-bottom: 1px solid #eee;
    .box {
      width: 60px;
      margin: 0 5px;
      display: inline-block;
      img {
        width: 120rpx;
        height: 120rpx;
        display: block;
        box-sizing: border-box;
      }
    }
    .active {
      border-bottom: 2px solid @theme-red;
    }
  }
  .current-head {
    background: #fff;
    padding: 16rpx 20rpx;
    border-bottom: 2rpx solid #eee;
    font-size: 26rpx;
    color: #666;
    text-align: center;
  }
  ::-webkit-scrollbar {
    width: 0;
    height: 0;
    color: transparent;
  }
}
</style>

